<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Button Click Dialog Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" type="text/css" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  #area { height: 100px; display: block; }
  #dialog { height: auto !important; }
  .ui-dialog { font-size: 10px; }
  .ui-dialog .ui-dialog-titlebar { padding: 2px 3px; height: 15px; }
  .ui-button { outline: 0; }
  .ui-dialog .ui-dialog-content { padding: 0px; margin: 0 5px; }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/ui_utils.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

var buttons = ['submit', 'okay', 'ok', 'yes', 'no', 'cancel', 'next', 'previous'];

var resetUI = function(div){
  if(div.html().length > 0) $('#dialog').dialog('destroy');
  div.empty(); // clear previous problem, if any
}

var createDialog = function(div){
  var html = '<p>' + ui_utils.generateWords(4,8) + '</p>';
  div.append(html);

  $('#dialog').dialog({ height: 70, position: {my: 'center', at: 'center', of: document.getElementById('area')} });

  $('.ui-dialog')[0].style.margin = core.randi(-10,20) + 'px ' + core.randi(5,25) + 'px';
  $('.ui-dialog')[0].style.width = core.randi(90,120) + 'px';
  $('.ui-dialog')[0].style.height = core.randi(70,100) + 'px';
}

var genProblem = function() {  
  // init: click dialog  
  var dialogDiv = $('#dialog');
  resetUI(dialogDiv);

  // init: click-button
  var div = d3.select('#area');
  div.html(''); // clear previous problem, if any

  // main: click-button
  var num = 6;
  var elements = [];
  for(var i=0;i<num;i++) {
    var u = core.randf(0,1);
    var must_make_button = (i === num-1) && elements.length === 0;

    if(u < 0.33 && !must_make_button) {
      var txt = ''; for(var q=0;q<3;q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
      div.append('div').html(txt);
    } else if(u < 0.66 && !must_make_button) {

      if(core.randf(0,1) < 0.5) {
        var txt = ''; for(var q=0;q<3;q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
        txt += ': ';
        div.append('span').html(txt);
      }

      var w = core.randi(40, 150);
      div.append('input').attr('type', 'text').attr('style', 'width:' + w + 'px;');
      div.append('br');
    } else {
      var btn_text = core.sample(buttons);
      if(core.randf(0,1) < 0.5) btn_text = ui_utils.txtCapitalize(btn_text);
      var btn = div.append('button').html(btn_text);
      if(core.randf(0,1) < 0.5) div.append('br');
      elements.push(btn);
    }

  }

  var correct_text = core.sample(elements).html();
  var buttonState = -1.0;
  for(var i = 0, len = elements.length; i < len; i++) {
    var e = elements[i];
    if(e.html() === correct_text) {
      e.on('click', function(){ buttonState = 1.0; console.log([correct_text, buttonState]);})
    } else {
      e.on('click', function(){ buttonState = -1.0; console.log([correct_text, buttonState]); })
    }
  }  

  // main: click-dialog  
  var dialogDiv = $('#dialog');
  if (dialogDiv.length == 0)  div.append('div').attr('id', 'dialog');
  var dialogDiv = $('#dialog');

  createDialog(dialogDiv);

  // $('#query').html('Close the dialog box by clicking the "x".');
  $('button.ui-button').on('click', function(){ 
    if (buttonState == 1.0){
      core.endEpisode(1.0, true);
    }else {
      core.endEpisode(-1.0);
    }
  })

  // generate query text in the UI
  // d3.select('#query').html('Click on the "' + correct_text + '" button, and then close the dialog box by clicking the "x".');
  d3.select('#query').html('Close the dialog box by clicking the "x", after clicking on the "' + correct_text + '" button.');
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area"><div id='dialog'></div></div>
</div>
</body>
</html>
